import { Dialog } from 'vant';
import { getSystem } from '@/utils/index'
const action = {
  methods: {
    getSystem,
    alert(obj) {
      return Dialog.alert({ title: '操作成功', message: JSON.stringify(obj) })
    },
    error(obj) {
      return Dialog.alert({ title: '操作失败', message: JSON.stringify(obj) })
    },
    startAction({action, params, handler, inValidSystem }) {
      if((inValidSystem || []).includes(this.getSystem()))
      return this.error('此功能在' + this.getSystem() + '环境下无效')
      /** 调用native api */ 
      let fn = this.$_native.api[action]
      typeof fn === 'function' && fn(...(params || [])).then(res => {
        if(handler) handler(res)
        else this.alert(res)
      }).catch((error) => {
        this.error(error)
      })  
    }
  }
}
export default action